package net.huashitong.user.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class UtilsTools {

    public final static String msgapiKey = "b3232c91e3336d52ae80b73bb0a1cb91";

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    //获取当当前年月日
    public static String getCurrentDate() {

        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String currentDate = sf.format(date);

        return currentDate;
    }

    //获取当前时间
    public static String getCurrentTime() {
		/*Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);*/
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
        String time = sf.format(date);
        return time;
    }

    //获取年份
    public static String getCurrenYear() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        String currentYear = sf.format(date);
        return currentYear;
    }

    //获取当前月份
    public static String getCurrentMonth() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String currentMonth = sf.format(date);
        return currentMonth;
    }

    //获取当前日期时间
    public static String getCurrentDateTime() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    /**
     * 字符串转日期 YYYYMMDD 格式
     *
     * @param StringDate
     * @return date
     */
    public static Date StringToDate(String StringDate) throws ParseException {
        // = new Date();
        Date date = sdf.parse(StringDate);
        return date;
    }

    /**
     * 日期转字符串 YYYMMDD
     *
     * @param date
     * @return
     */
    public static String DateToString(Date date) {

        String Stringdate = sdf.format(date);
        return Stringdate;
    }

    /**
     * 日期加天数的日期
     *
     * @param num
     * @param StringDate
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num, String StringDate) throws ParseException {
        Date date = sdf.parse(StringDate);
        System.out.println("现在日期为：" + date);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, num);
        date = cal.getTime();
        String currentDate = sdf.format(date);
        System.out.println("改变后的日期为：" + currentDate);
        return currentDate;
    }

    //** 保留小数
    public static Double keepNumPostion(int x, double num) {
        BigDecimal bg = new BigDecimal(num);
        double numFormat = bg.setScale(x, BigDecimal.ROUND_HALF_UP).doubleValue();
        return numFormat;
    }

    //日期格式化
    public static String formatDate(String dates) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMM");

        String formatdate = "";

        try {
            Date parse = sf2.parse(dates);
            formatdate = sf.format(parse);


        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatdate;
    }


    public static double formatWan(double value, int x) {
        if (x >= 0) {
            for (int i = 0; i < x; i++) {
                value = value / 10;
            }
        }else{
            for (int i = 0; i < -x; i++) {
                value = value * 10;
            }
        }
        return value;
    }


}
